package com.lian.hope.controller;

import com.lian.hope.annotations.RequiredPermission;
import com.lian.hope.enums.PermissionType;
import com.lian.hope.model.dto.ProjectsDTO;
import com.lian.hope.model.dto.ProjectsDonationsDTO;
import com.lian.hope.model.vo.PageBean;
import com.lian.hope.model.vo.ProjectsVO;
import com.lian.hope.service.ProjectsService;
import com.lian.hope.utils.JwtUtil;
import com.lian.hope.utils.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/projects")
@Slf4j
@RequiredArgsConstructor
@Tag(name = "公益项目模块相关接口", description = "公益项目模块相关接口")
public class ProjectsController {
    private final ProjectsService projectsService;

    /**
     * 分页获取项目列表
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping
    @Operation(summary = "分页获取项目列表")
    @Parameters({
        @Parameter(description = "页码"),
        @Parameter(description = "每页数量")
    })
    public Result<PageBean<ProjectsVO>> listProjects(Integer pageNum,

        Integer pageSize) {
        PageBean<ProjectsVO> projects = projectsService.listProjects(pageNum, pageSize);
        return Result.success(projects);
    }


    @GetMapping("/{projectId}/detail")
    @Operation(summary = "根据id获取项目详情")
    public Result<ProjectsVO> getProjectsById( @Parameter(description = "项目id",example = "1") @PathVariable Long projectId) {
        ProjectsVO projects = projectsService.getProjectById(projectId);
        return Result.success(projects);
    }

    /**
     * 新增项目
     *
     * @param projects
     * @return
     */
    @PostMapping
    @RequiredPermission(PermissionType.PROJECT)
    @Operation(summary = "新增项目")
    public Result insertProjects(
        @RequestBody ProjectsDTO projects, @RequestHeader String Authorization
                                ) {


                Map<String, Object> claims = JwtUtil.parseToken(Authorization);
        Long userId = Long.parseLong(claims.get("userId").toString());
        projectsService.insertProjects(projects,userId);
        return Result.success();
    }

    /**
     * 随机获取一个项目
     *
     * @return
     */
    @GetMapping("/random")
    @Operation(summary = "随机获取一个项目")
    public Result<ProjectsVO> getRandomProjects() {
        ProjectsVO projects = projectsService.getRandomProjects();
        return Result.success(projects);
    }

    @PostMapping("/{projectId}/donations")
    @Operation(summary = "项目捐款")
    public Result insertDonations(@RequestBody ProjectsDonationsDTO projectsDonationsDTO,
        @RequestHeader String Authorization, @PathVariable Long projectId) {

        Map<String, Object> claims = JwtUtil.parseToken(Authorization);
        Long userId = Long.parseLong(claims.get("userId").toString());

        projectsService.insertDonations(projectId,projectsDonationsDTO,userId);
        return Result.success();
    }

}
